<?php
namespace My\Controller;

use Utuo\BackgroundController;
use My\Model\ProductOrderModel;

class ProductDiliverymanController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle("配送员管理");
		$this->setActionTitle(array(
			'index' => '配送员列表',
			'add' => '配送员添加',
			'recycle' => '回收站',
			'orders' => '配送订单列表',
		));
		$this->setActionNav("index,add,recycle");
	}
	
	public function getLists($is_deleted) {
		$condition = array(
			'a.cid' => $this->aid,
			'a.is_deleted' => $is_deleted );
		$d = D('ProductDiliveryman a')->join("__USER__ b on a.uid=b.id");
		$n = clone $d;
		$productDiliveryman = $d->where($condition)->page($this->getPage(), $this->getPageSize())->field("a.id,a.uid,b.name,b.email,b.nick_name")->select();
		$count = $n->where($condition)->count();
		$this->showPage($count, $this->getPageSize());
		$this->assign('productDiliverymanLists', $productDiliveryman);
		$this->display();
	}
	
	public function addAction() {
		$this->display();
	}
	
	public function addPostAction() {
		$uid = I("uid", 0, "intval") or $this->ajaxError("请选择用户");
		$user = M("User a")->join("__USER_ASSOCIATION__ b on a.id=b.uid and b.cid={$this->aid}")->where(array("a.id" => $uid))->find() or $this->ajaxError("选择的用户不是当前商家的注册用户.");
		$m = M("ProductDiliveryman");
		$m->where("cid=%d and uid=%d", $this->aid, $uid)->find() and $this->ajaxError("选择的用户已经添加过了,请不要重复添加.");
		$m->add(array('uid'=>$uid, 'cid'=>$this->aid)) and $this->ajaxSuccess("添加成功");
		$this->ajaxSuccess("添加失败");
	}
	
	/**
	 * 获得当前店铺的所有注册用户
	 */
	public function getUserListAction() {
		$name = I("name", ""); 
		$userList = M("User a")->join("__USER_ASSOCIATION__ b on a.id=b.uid")
							   ->where(array("_complex" => array("a.name"=>array("like", "%".$name."%"),
													   		"a.nick_name"=>array("like", "%".$name."%"),
													   		"_logic" => 'or'),
							   				"b.is_deleted" => 0,
							   				"b.cid" => $this->aid,
							   		  ) )
							   ->field("a.*")
							   ->select();
		$this->ajaxSuccess("", $userList);
	}
	
	/**
	 * 分配给配送员的订单
	 */
	public function ordersAction() {
		$uid = I('uid', 0, "intval");
		$productOrderLists = M("ProductOrder orders")->join ( "__CN_PROVINCE__ province on orders.province = province.code", "LEFT" )
												  ->join ( "__CN_CITY__ city on orders.city = city.code", "LEFT" )
												  ->join ( "__CN_AREA__ area on orders.area = area.code", "LEFT" )
												  ->join( "__PRODUCT_DELIVER_DISTRIBUTION__ dis on dis.uid={$uid} and dis.order_id=orders.id", "LEFT" )
												  ->where(array("orders.cid" => $this->aid, "orders.deliverman_uid" => $uid, "orders.deliver_method" => 1))
												  ->field ( "orders.*,dis.id as disId,dis.is_collection,province.name as provinceName, city.name as cityName, area.name as areaName" )
												  ->select();
		$this->assign("productOrderLists", $productOrderLists);
		$this->display();
	}
	
	/**
	 * 撤消分配的订单
	 */
	public function cancelAction() {
		$id = I("id", 0, "intval");
		$deliverDistribution = D("ProductDeliverDistribution")->getOneById( $id );
		// 更新订单状态
		false !== D("ProductOrder")->where(array('id'=>$deliverDistribution['order_id']))
						 ->save(array('shipping_time' => 0, 'deliverman_uid' => 0, 'deliver_method' => 0, 'order_status' => ProductOrderModel::ORDER_STATUS_SHIPPING))
		and // 删除分配订单
		D("ProductDeliverDistribution")->where(array('id'=>$id))->delete() and $this->ajaxSuccess("撤消成功!");
		$this->ajaxError("撤消失败!");
	}
	
	/**
	 * 设置收款状态
	 */
	public function setCollectionStatusAction() {
		$this->ajaxSetStatus("ProductDeliverDistribution", "is_collection");
	}
	
}